package org.example.service.impl;

import org.example.mapper.CategoryMapper;
import org.example.pojo.Category;
import org.example.pojo.Result;
import org.example.service.CategoryService;
import org.example.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;
    @Override
    public Result add(Category category) {
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        category.setCreateUser(userId);
        categoryMapper.add(category);
        return Result.success("操作成功");
    }

    @Override
    public Result<List<Category>> list() {
        List<Category> list = categoryMapper.list();
        return Result.success(list);
    }

    @Override
    public Result<Category> detail(Integer id) {
        Category c = categoryMapper.findById(id);
        return Result.success(c);
    }

    @Override
    public Result update(Category category) {
        categoryMapper.update(category);
        return Result.success("更新成功");
    }

    @Override
    public Result delete(Integer id) {
        categoryMapper.deleteById(id);
        return Result.success("删除成功");
    }
}
